<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>call,apply</title>
</head>
<body>
<script>
/*

    function Person(name,age){
        //下面传过来的obj，相当于 this = obj
        this.name = name;// obj.name = name;
        this.age = age;//obj.age = age;
    }
    var person = new Person("han",18);
    var obj = {};
    //Person.call()与Person()没有啥区别
    Person.call(obj);//借助Person来封装obj
    console.log(obj);//Object ---> age: undefined   name: undefined  __proto__: Object
    //这里的call会把Person里中所有的this.***加到obj中去
    Person.call(obj,"Lu",20);
    console.log(obj);//{name: "Lu", age: 20} age: 20  name: "Lu"  __proto__: Object
    //call(a,b,c) //第一个参数是改变this的指向  第二个以及后面的参数会当做正常的实参传到形参里面去

 */

//使用call来增强效率
    function Person(name,age,sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    }
    // function Student(name,age,sex,tel,grand) {
    //     this.name = name;
    //     this.age = age;
    //     this.sex = sex;
    //     this.tel = tel;
    //     this.grand = grand;
    // }
    function Student(name,age,sex,tel,grand) {
        //var this = {};
        Person.call(this,name,age,sex);//.call 就是借用别人的函数来实现自己，就相当于把那三个语句拿到这里来了
        //var this = {name:"",age:"",sex:""}//封装完之后就会有Person的劳动成果
        this.tel = tel;
        this.grand = grand;
    }
    var student = new Student("sunny",18,"male",15885336262,2017);


//一个汽车生产工厂

    function Wheel(wheelSize,style) {
        this.wheelSize = wheelSize;
        this.style = style;
    }
    function Sit(c,sitColor) {
        this.c = c;
        this.sitColor = sitColor;
    }
    function Model(height,width,len) {
        this.height = height;
        this.width = width;
        this.len = len;
    }
    function Car(wheelSize,style,c,sitColor,height,width,len) {
        //相当于调用其他的成果
        Wheel.call(this,wheelSize,style);
        Sit.call(this,c,sitColor);
        Model.call(this,height,width,len);
    }
    var car = new Car(1000,"beautiful","真皮","yellow",2000,1800,4900);
    console.log(car);//Car {wheelSize: 1000, style: "beautiful", c: "真皮", sitColor: "yellow", height: 2000, …}

//call 第一位需要传this 后面需要把实参按照形参的个数传进去
//apply 第一位需要传this 后面需要传一个 arguments 进去，就是传一个数组
    function testCar(wheelSize,style,c,sitColor,height,width,len) {
        //相当于调用其他的成果
        Wheel.apply(this,[wheelSize,style]);
        Sit.apply(this,[c,sitColor]);
        Model.apply(this,[height,width,len]);
    }
    var tcar = new Car(900,"cute","真真皮皮","red",1000,1600,4500);
    console.log(tcar);//Car {wheelSize: 900, style: "cute", c: "真真皮皮", sitColor: "red", height: 1000, …}

</script>
</body>
</html>
